package cn.javaxxw.springboot.controller;

import cn.javaxxw.springboot.entity.User;
import cn.javaxxw.springboot.service.UserService;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能描述: 
 * @author  TuYong
 * @date  2022/5/31 16:32
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    @GetMapping("/page")
    public Map<String,Object> page(@RequestParam(required = false,defaultValue = "1",value = "pageNum") int pageNum,
                                    @RequestParam(required = false,defaultValue = "10",value = "pageSize") int pageSize){
        Map<String,Object> resultMap = new HashMap<>(8);
        PageInfo<User> page =  userService.page(pageNum,pageSize);
        resultMap.put("data",page);
        resultMap.put("code",10000);
        resultMap.put("msg","处理成功");
        return resultMap;
    }

    @GetMapping("/getAll")
    public Map<String,Object> getAll(){
        Map<String,Object> resultMap = new HashMap<>(8);
        resultMap.put("data",userService.getAll());
        resultMap.put("code",10000);
        resultMap.put("msg","处理成功");
        return resultMap;
    }

    @GetMapping("/queryByUserId")
    public Map<String,Object> queryByUserId(@RequestParam("userId") Long userId){
        Map<String,Object> resultMap = new HashMap<>(8);
        resultMap.put("data",userService.findById(userId));
        resultMap.put("code",10000);
        resultMap.put("msg","处理成功");
        return resultMap;
    }

    @PostMapping("save")
    public Map<String,Object> save(@RequestBody User user){
        Map<String,Object> resultMap = new HashMap<>(8);
        resultMap.put("data",userService.saveUser(user));
        resultMap.put("code",10000);
        resultMap.put("msg","处理成功");
        return resultMap;
    }

}